package p270jp.pxv.android.upload;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
import java.io.File;
import java.util.Objects;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.internal.http2.Http2Connection;
import p014ae.C0135p4;
import p014ae.C0147r4;
import p014ae.C0180w6;
import p016ag.PixivAppApiErrorMapper;
import p090d0.C2518n;
import p133eh.IllustUploadRepository;
import p134ei.PixivAnalytics;
import p158fh.IllustUploadRequest;
import p248j2.C3918n;
import p248j2.NotificationManagerCompat;
import p270jp.pxv.android.R;
import p270jp.pxv.android.commonObjects.model.PixivAppApiError;
import p270jp.pxv.android.model.pixiv_sketch.LiveWebSocketMessage;
import p274k2.ContextCompat;
import p325m0.MutabilityOwnership;
import p422ph.IllustUploadRequestConverter;
import p422ph.IllustUploadService;
import p491sd.C6034a;
import p569vd.C6830b;
import p571vf.OkHttpWrapper;
import p639y6.C7296l;
import p639y6.C7298n;
import p639y6.C7301s;
import p645yc.AndroidSchedulers;
import p665z6.C7460o;
import p671zc.CompositeDisposable;
import p674zf.ConvertKey;
import p674zf.IllustUploadParameter;

/* renamed from: jp.pxv.android.upload.IllustUploadPollingService */
/* loaded from: classes2.dex */
public class IllustUploadPollingService extends Service {

    /* renamed from: i */
    public static final /* synthetic */ int f19386i = 0;

    /* renamed from: a */
    public NotificationManagerCompat f19387a;

    /* renamed from: c */
    public ConvertKey f19389c;

    /* renamed from: e */
    public IllustUploadParameter f19391e;

    /* renamed from: f */
    public PixivAnalytics f19392f;

    /* renamed from: g */
    public IllustUploadService f19393g;

    /* renamed from: h */
    public PixivAppApiErrorMapper f19394h;

    /* renamed from: b */
    public int f19388b = 0;

    /* renamed from: d */
    public CompositeDisposable f19390d = new CompositeDisposable();

    /* renamed from: a */
    public final C3918n m4537a(PendingIntent pendingIntent) {
        C3918n nVar = new C3918n(getApplicationContext(), "default_notification_channel_id");
        nVar.f17891q = 1;
        nVar.f17882h = pendingIntent;
        nVar.m4978e(128, false);
        nVar.f17881g = pendingIntent;
        nVar.f17894t.icon = R.drawable.ic_stat_notification;
        Context applicationContext = getApplicationContext();
        Object obj = ContextCompat.f19963a;
        nVar.f17890p = ContextCompat.C4473d.m4257a(applicationContext, R.color.push_notification_icon);
        nVar.m4978e(16, true);
        return nVar;
    }

    /* renamed from: b */
    public final void m4536b() {
        CompositeDisposable aVar = this.f19390d;
        IllustUploadService fVar = this.f19393g;
        ConvertKey aVar2 = this.f19389c;
        Objects.requireNonNull(fVar);
        MutabilityOwnership.m3745m(aVar2, "convertKey");
        IllustUploadRepository aVar3 = fVar.f24702a;
        Objects.requireNonNull(aVar3);
        IllustUploadRequest aVar4 = aVar3.f13518a;
        Objects.requireNonNull(aVar4);
        aVar.mo82c(aVar4.f14639a.mo1127a().m970f(new C7460o(aVar4, aVar2, 3)).m967i(new C7296l(aVar3, 16)).m966j(AndroidSchedulers.m334a()).m963m(new C0147r4(this, 17), new C0180w6(this, 13)));
    }

    /* renamed from: c */
    public final void m4535c(PixivAppApiError pixivAppApiError) {
        String string = getString(R.string.upload_notification_reupload);
        if (!(pixivAppApiError == null || pixivAppApiError.getUserMessageDetails() == null || pixivAppApiError.getUserMessageDetails().size() <= 0)) {
            string = pixivAppApiError.getUserMessageDetails().values().iterator().next();
        }
        C3918n a = m4537a(PendingIntent.getActivity(getApplicationContext(), 0, IllustUploadActivity.f19355K.m4538b(getBaseContext(), this.f19391e, pixivAppApiError), 134217728));
        a.m4979d(getString(R.string.upload_notify_failed));
        a.m4980c(string);
        a.m4974i(getString(R.string.upload_notify_failed));
        m4534d(a.m4982a());
        stopSelf();
    }

    /* renamed from: d */
    public final void m4534d(Notification notification) {
        NotificationManagerCompat rVar = this.f19387a;
        Objects.requireNonNull(rVar);
        Bundle bundle = notification.extras;
        if (bundle != null && bundle.getBoolean("android.support.useSideChannel")) {
            NotificationManagerCompat.C3920a aVar = new NotificationManagerCompat.C3920a(rVar.f17909a.getPackageName(), Http2Connection.DEGRADED_PONG_TIMEOUT_NS, null, notification);
            synchronized (NotificationManagerCompat.f17907f) {
                if (NotificationManagerCompat.f17908g == null) {
                    NotificationManagerCompat.f17908g = new NotificationManagerCompat.ServiceConnectionC3922c(rVar.f17909a.getApplicationContext());
                }
                NotificationManagerCompat.f17908g.f17918b.obtainMessage(0, aVar).sendToTarget();
            }
            rVar.f17910b.cancel(null, Http2Connection.DEGRADED_PONG_TIMEOUT_NS);
        } else {
            rVar.f17910b.notify(null, Http2Connection.DEGRADED_PONG_TIMEOUT_NS, notification);
        }
        Toast.makeText(getApplicationContext(), notification.tickerText, 0).show();
    }

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        this.f19387a = new NotificationManagerCompat(getApplicationContext());
        this.f19392f = (PixivAnalytics) C2518n.m7641d(PixivAnalytics.class);
        this.f19393g = (IllustUploadService) C2518n.m7641d(IllustUploadService.class);
        this.f19394h = (PixivAppApiErrorMapper) C2518n.m7641d(PixivAppApiErrorMapper.class);
        C3918n a = m4537a(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 134217728));
        a.m4979d(getString(R.string.upload_notification_uploading));
        a.m4980c(getString(R.string.upload_notification_wait));
        a.m4974i(getString(R.string.upload_notification_uploading));
        startForeground(867374626, a.m4982a());
    }

    @Override // android.app.Service
    public void onDestroy() {
        this.f19390d.m80f();
        super.onDestroy();
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i, int i2) {
        IllustUploadParameter bVar = (IllustUploadParameter) intent.getSerializableExtra("UPLOAD_PARAMETER");
        this.f19391e = bVar;
        CompositeDisposable aVar = this.f19390d;
        IllustUploadService fVar = this.f19393g;
        Objects.requireNonNull(fVar);
        MutabilityOwnership.m3745m(bVar, "illustUploadParameter");
        IllustUploadRepository aVar2 = fVar.f24702a;
        Objects.requireNonNull(aVar2);
        IllustUploadRequestConverter eVar = aVar2.f13519b;
        Objects.requireNonNull(eVar);
        Objects.requireNonNull(eVar.f24700a);
        MultipartBody.Builder builder = new MultipartBody.Builder(null, 1, null);
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("title", bVar.f31457a);
        builder.addFormDataPart(LiveWebSocketMessage.TYPE_CAPTION, bVar.f31458b);
        builder.addFormDataPart("type", bVar.f31459c);
        builder.addFormDataPart("restrict", bVar.f31461e);
        builder.addFormDataPart("x_restrict", bVar.f31460d);
        builder.addFormDataPart("is_sexual", String.valueOf(bVar.f31462f));
        for (String str : bVar.f31464h) {
            builder.addFormDataPart("tags[]", str);
        }
        for (String str2 : bVar.f31463g) {
            Objects.requireNonNull(eVar.f24701b);
            MutabilityOwnership.m3745m(str2, "path");
            File file = new File(str2);
            String name = file.getName();
            Objects.requireNonNull(eVar.f24700a);
            builder.addFormDataPart("files[]", name, RequestBody.Companion.create(file, OkHttpWrapper.f29119a));
        }
        builder.addFormDataPart("comment_access_control", String.valueOf(bVar.f31465i.getValue()));
        MultipartBody build = builder.build();
        IllustUploadRequest aVar3 = aVar2.f13518a;
        Objects.requireNonNull(aVar3);
        MutabilityOwnership.m3745m(build, "body");
        aVar.mo82c(aVar3.f14639a.mo1127a().m970f(new C7298n(aVar3, build, 7)).m967i(new C7301s(aVar2, 15)).m961o(C6034a.f26179d).m966j(AndroidSchedulers.m334a()).m963m(new C0135p4(this, 18), new C6830b(this, 17)));
        return 2;
    }
}
